package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.vo.PartsManagement.BusinessDepartmentType;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.bean.vo.bc.UserDepartments;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessDepartmentTypeService;
import com.cn.service.server.PartsManagement.BusinessTypeService;
import com.cn.service.server.admin.UserDepartmentsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Bdy")
@Transactional
public class BusinessDepartmentTypeController {
    private final BusinessDepartmentTypeService businessDepartmentTypeService;

    @Autowired(required = false)
    public BusinessDepartmentTypeController(BusinessDepartmentTypeService businessDepartmentTypeService) {
        this.businessDepartmentTypeService = businessDepartmentTypeService;
    }
    @RequestMapping("/select/BusinessDepartmentType")
    @ResponseBody
    public List<BusinessDepartmentType> selectBusinessDepartmentType(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("inputName");
        name = (name != null) ? name : "";
        return businessDepartmentTypeService.selectBusinessDepartmentType(name,page,limit);
    }
    @RequestMapping("/select/Departments")
    @ResponseBody
    public List<UserDepartments> selectDepartments(@RequestBody JSONObject jsonObject) {
        return businessDepartmentTypeService.selectDepartments();
    }
    @RequestMapping("/select/UserDepartments/BusinessDepartmentType")
    @ResponseBody
    public List<User> selectUserDepartmentsBusinessDepartmentType(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");


        return businessDepartmentTypeService.selectUserDepartmentsBusinessDepartmentType(id);
    }
    @RequestMapping("/add/BusinessDepartment/Type")
    @ResponseBody
    public Result addBusinessDepartmentType(@RequestBody JSONObject jsonObject) {
        Integer businessTypeID = jsonObject.getInteger("businessTypeID");
        Integer organizationType = jsonObject.getInteger("organizationType");
        String description = jsonObject.getString("description");
        Integer departmentID = jsonObject.getInteger("departmentID");
//        Integer ID = jsonObject.getInteger("ID");
        Integer status = jsonObject.getInteger("status");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if(childList.size()==0){
           return Result.failure("500","请填写业务人员");
        }
        List<BusinessDepartmentType> teamPersonnelList = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            int userId = childObject.getInteger("userId");
            String description1 = childObject.getString("description");
            Integer status1 = childObject.getInteger("status");
            BusinessDepartmentType businessDepartmentType = new BusinessDepartmentType();
            businessDepartmentType.setUserId(userId);
            businessDepartmentType.setDescription(description1);
            businessDepartmentType.setStatus(status1);
            teamPersonnelList.add(businessDepartmentType);
        }
        return businessDepartmentTypeService.addBusinessDepartmentType(departmentID,businessTypeID,description,status,organizationType,teamPersonnelList);
    }
    @RequestMapping("/delete/BusinessDepartmentType/Mapping")
    @ResponseBody
    public Result deleteBusinessDepartmentTypeMapping(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<BusinessDepartmentType> idList = new ArrayList<>();
        for (int i = 0; i < deleteIdList.size(); i++) {
            JSONObject deleteIdObject = deleteIdList.getJSONObject(i);
            int id = deleteIdObject.getInteger("departmentID");
            BusinessDepartmentType personnel = new BusinessDepartmentType();
            personnel.setDepartmentID(id);
            idList.add(personnel);
        }
        return businessDepartmentTypeService.deleteBusinessDepartmentTypeMapping(idList);
    }
    @RequestMapping("/update/BusinessDepartmentType/Mapping")
    @ResponseBody
    public Result updateBusinessDepartmentTypeMapping(@RequestBody JSONObject jsonObject) {
        Integer businessTypeID = jsonObject.getInteger("businessTypeID");
        Integer organizationType = jsonObject.getInteger("organizationType");
        String description = jsonObject.getString("description");
        Integer departmentID = jsonObject.getInteger("departmentID");
        Integer ID = jsonObject.getInteger("ID");
        Integer status = jsonObject.getInteger("status");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if(childList.size()==0){
            Result.failure("500","请填写业务人员");
        }
        List<BusinessDepartmentType> teamPersonnelList = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            int userId = childObject.getInteger("userId");
            String description1 = childObject.getString("description");
            Integer status1 = childObject.getInteger("status");
            BusinessDepartmentType businessDepartmentType = new BusinessDepartmentType();
            businessDepartmentType.setUserId(userId);
            businessDepartmentType.setDescription(description1);
            businessDepartmentType.setStatus(status1);
            teamPersonnelList.add(businessDepartmentType);
        }
        return businessDepartmentTypeService.updateBusinessDepartmentTypeMapping(ID,departmentID,businessTypeID,description,status,organizationType,teamPersonnelList);
    }
}
